package com.czy.controller;

import java.util.HashMap;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("msg")
public class MQSendMsgController {

	
	@Autowired
	private JmsTemplate jmsTemplate;
	
	/**
	 * 发送队列
	 */
	@RequestMapping("sendQueue")
	public Map<String,Object> sendQueue(){
		Map<String,Object> map=new HashMap<String, Object>();
		jmsTemplate.setTimeToLive(1);
		jmsTemplate.setDeliveryPersistent(false);
		jmsTemplate.setDefaultDestination(new ActiveMQQueue("queue-hello"));
		jmsTemplate.send(new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				System.out.println("队列发送成功");
				return session.createTextMessage("hello-queue-msg-setTimeToLive");
			}
		});
		map.put("success", "发送成功");
		return map;
	}
	
	/**
	 * 发送主题
	 */
	@RequestMapping("sendTopic")
	public Map<String,Object> sendTopic(){
		Map<String,Object> map=new HashMap<String, Object>();
		
		jmsTemplate.setDefaultDestination(new ActiveMQTopic("topic-hello"));
		jmsTemplate.send(new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				System.out.println("主题发送成功");
				return session.createTextMessage("hello-topic-msg");
			}
		});
		map.put("success", "发送成功");
		return map;
	}
	
	
}
